//
//  BannarView.m
//  BannarPic
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "BannarView.h"
#import "BannarCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface BannarView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, strong)NSMutableArray *myArr;
@property(nonatomic, strong)UICollectionView *myCollectionView;
@property(nonatomic, strong)NSTimer *myTimer;
@property(nonatomic, strong)UIPageControl *myPageControl;

@end
@implementation BannarView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *myFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        myFlowLayout.itemSize = CGSizeMake(WIDTH, 250);
        myFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        myFlowLayout.minimumLineSpacing = 0;
        
        self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 250) collectionViewLayout:myFlowLayout];
        [self addSubview:_myCollectionView];
        self.myCollectionView.delegate = self;
        self.myCollectionView.dataSource = self;
        self.myCollectionView.pagingEnabled = YES;
        self.myCollectionView.backgroundColor = [UIColor cyanColor];
        [self.myCollectionView registerClass:[BannarCell class] forCellWithReuseIdentifier:@"cell"];
        
        
        self.myPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 20, WIDTH / 2, 20)];
        self.myPageControl.center = CGPointMake(WIDTH / 2, frame.size.height - 20);
        self.myPageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
        self.myPageControl.pageIndicatorTintColor = [UIColor whiteColor];
        [self addSubview:self.myPageControl];
        [self addTimer];
    }
    return self;
}

- (void)setPicArr:(NSMutableArray *)picArr{
    if (_picArr != picArr) {
        _picArr = picArr;
    }
    [_myCollectionView reloadData];
    self.myPageControl.numberOfPages = self.picArr.count;
//    [self addTimer];

}

- (void)addTimer{
    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(scrollNext) userInfo:nil repeats:YES];
    // 问题:定时器是不是一定准确?
    // 因为"RunLoop"导致,当定时器加到"scrollView"或者是继承于"scrollView"的控件上的时候,当滑动"scrollView"的时候,定时器会停止
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    self.myTimer = timer;
}

- (NSIndexPath *)returnIndex{
    NSIndexPath *currentIndex = [[self.myCollectionView indexPathsForVisibleItems] lastObject];
    currentIndex = [NSIndexPath indexPathForItem:currentIndex.item inSection:100 / 2];
    [self.myCollectionView scrollToItemAtIndexPath:currentIndex atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    return currentIndex;
}

- (void)scrollNext{
    // 获取当前显示"cell"的"indexPath"
//    NSIndexPath *currentIndex = [[self.myCollectionView indexPathsForVisibleItems] lastObject];
    NSIndexPath *currentIndex = [self returnIndex];
    NSInteger item = currentIndex.item + 1;
    NSInteger section = currentIndex.section;
    
    if (item == self.picArr.count) {
        item = 0;
        section ++;
    }
    
//    if (section == 100) {
//        item = 0;
//        section = 0;
//    }
    
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:item inSection:section];

    [self.myCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    self.myPageControl.currentPage  = item;
}

- (void)removeTimer{
    if (self.myTimer != nil) {
        // 停止定时器
        [self.myTimer invalidate];
        // 定时器置空
        self.myTimer = nil;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 100;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.myPageControl.currentPage = (NSInteger) (scrollView.contentOffset.x / WIDTH) % self.picArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BannarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor cyanColor];
    cell.listModel = [self.picArr objectAtIndex:indexPath.item];
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
